#include "CampaignModellingForm.h"
#include "ui_CampaignModellingForm.h"



#include "SkillProxyModel.h"
#include "FeatProxyModel.h"
#include "ReferenceProxyModel.h"
#include "SynergyProxyModel.h"



#include "ItemDelegate.h"
#include "SkillSynergyDelegate.h"



#include <ItemRoot.h>



#include <QDebug>



CampaignModellingForm::CampaignModellingForm(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::CampaignModellingForm)
{
    m_model = 0;
    ui->setupUi(this);

    initialize();
}



CampaignModellingForm::CampaignModellingForm(ItemModel *model, QWidget *parent) :
        QWidget(parent),
        ui(new Ui::CampaignModellingForm)
{
    m_model = 0;
    ui->setupUi(this);

    initialize();

    setModel(model);
}



CampaignModellingForm::~CampaignModellingForm()
{
    delete ui;
}



ItemModel* CampaignModellingForm::model() const
{
    return m_model;
}



void CampaignModellingForm::setModel(ItemModel *model)
{
    qDebug(Q_FUNC_INFO);

    m_model = model;
    if (!m_model) return;

    m_model->setParent(this);
    ui->treeViewData->setModel(m_model);

    ui->lineEditName->setText(m_model->rootItem()->name());

    // skills
    SkillProxyModel* skillProxy = new SkillProxyModel(this);
    skillProxy->setSourceModel(m_model);
    ui->treeViewSkills->setModel(skillProxy);

    // skill synergies
    SynergyProxyModel* synergyProxy = new SynergyProxyModel(this);
    synergyProxy->setSourceModel(m_model);
    ui->treeViewSynergies->setModel(synergyProxy);

    // feats
    FeatProxyModel* featProxy = new FeatProxyModel(this);
    featProxy->setSourceModel(m_model);
    ui->treeViewFeats->setModel(featProxy);

    if (!m_model->rootItem()->isDirty()) return;

    on_pushButtonName_clicked();
}



void CampaignModellingForm::initialize()
{
    // raw data tree
    ui->treeViewData->setItemDelegate(new ItemDelegate(this));

    // skill tree
    ItemDelegate* delegate = new ItemDelegate(this);
    delegate->setEditorFilter(SkillProxyModel::RefField, ".Attribute");
    ui->treeViewSkills->setItemDelegate(delegate);

    // skill synergies
    //ui->treeViewSynergies->setItemDelegate(new ItemDelegate(this));

    // feat tree
    //ui->treeViewFeats->setItemDelegate(new ItemDelegate(this));
}



void CampaignModellingForm::on_pushButtonName_clicked()
{
    qDebug(Q_FUNC_INFO);

    ui->lineEditName->setReadOnly(false);

    ui->lineEditName->setFocus();
    ui->lineEditName->selectAll();

}



void CampaignModellingForm::on_treeViewSkills_clicked(QModelIndex index)
{
    qDebug() << Q_FUNC_INFO;
    on_treeViewSkills_activated(index);
}



void CampaignModellingForm::on_treeViewSkills_activated(QModelIndex proxyIndex)
{
    qDebug() << Q_FUNC_INFO;
    if (!proxyIndex.isValid()) return;

    QModelIndex nameProxy = proxyIndex.sibling(proxyIndex.row(), SynergyProxyModel::NameField);
    if (!nameProxy.isValid()) return;

    qDebug("skill proxy check");
    SkillProxyModel* skillModel = dynamic_cast<SkillProxyModel*>(ui->treeViewSkills->model());
    if (!skillModel) return;

    qDebug("source index check");
    QModelIndex sourceIndex = skillModel->mapToSource(nameProxy);
    if (!sourceIndex.isValid()) return;

    qDebug("source category check");
    QModelIndex sourceCategoryIndex = sourceIndex.sibling(sourceIndex.row(), AbstractItem::CategoryData);
    if (!sourceCategoryIndex.isValid())
    {
        qDebug("invalid category index");
        return;
    }

    if (sourceCategoryIndex.data(Qt::EditRole).toInt() != ItemCategory_Item)
    {
        qDebug("Category is not item!");
        return;
    }

    QModelIndex sourceIdIndex = sourceIndex.sibling(sourceIndex.row(), AbstractItem::IdData);


    qDebug("reference proxy check");
    ReferenceProxyModel* synergyModel = dynamic_cast<ReferenceProxyModel*>(ui->treeViewSynergies->model());
    if (!synergyModel) return;

    qDebug() << "setting source target:" << sourceIdIndex.data(Qt::EditRole).toString();
    synergyModel->setSourceTarget(sourceIdIndex);
}


